package com.dha.asynctask;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.Toast;

import com.dha.variable.DangNhapVariable;
import com.dha.variable.ThongBaoVariable;
import com.dha.variable.ThongTinTaiKhoan;

public class DangNhapAsyncTask extends AsyncTask<Void, Void, Boolean> {
	private ProgressDialog dialog;
	private Activity activity;
	private String maSo, matKhau;

	public DangNhapAsyncTask(Activity activity, String maSo, String matKhau) {
		super();
		this.activity = activity;
		this.maSo = maSo;
		this.matKhau = matKhau;
	}

	@Override
	protected void onPreExecute() {
		dialog = ProgressDialog.show(activity, "Vui lòng chờ",
				"Đang đăng nhập vào tài khoản " + maSo);
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(Boolean result) {
		if (result) {
			if(maSo.length() == 7){
				Intent intentGiaoVien = new Intent("GiaoVienDaDangNhap");
				activity.startActivity(intentGiaoVien);
			}else{
				if (maSo.length() == 12){
					Intent intentSinhVien = new Intent("SinhVienDaDangNhap");
					activity.startActivity(intentSinhVien);
				}
			}
			dialog.dismiss();

		} else {
			dialog.dismiss();
			Toast.makeText(
					activity,
					"Đăng nhập không thành công\nMã tài khoản hoặc mật khẩu không đúng",
					Toast.LENGTH_LONG).show();
		}
		super.onPostExecute(result);
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		if ((maSo.length() == 7 || maSo.length() == 12) && matKhau.length() > 4){
			try {
				SoapObject request = new SoapObject(DangNhapVariable.NAME_SPACE_DANGNHAP, DangNhapVariable.METHOD_NAME_DANGNHAP);
				PropertyInfo maSoinfo = new PropertyInfo();
				maSoinfo.setName("MaSo");
				maSoinfo.setValue(maSo);

				PropertyInfo matKhauinfo = new PropertyInfo();
				matKhauinfo.setName("MatKhau");
				matKhauinfo.setValue(matKhau);
				request.addProperty(maSoinfo);
				request.addProperty(matKhauinfo);
				
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				envelope.setOutputSoapObject(request);
				
				HttpTransportSE httpTransportSE = new HttpTransportSE(
						DangNhapVariable.URL_DANGNHAP);
				httpTransportSE.call(DangNhapVariable.SOAP_ACTION_DANGNHAP, envelope);
				
				SoapPrimitive respone = (SoapPrimitive) envelope.getResponse();
				//Nếu kết quả trả về là 1 <=> số dòng được selecr ra , đúng
				if(respone.toString().equals("1")){
					try {
						//Nếu đăng nhập ok thì tiến hành lấy ra thông tin mã tài khaosrn và họ tên
						SoapObject requestThongTin = new SoapObject(DangNhapVariable.NAME_SPACE_DANGNHAP, DangNhapVariable.METHOD_NAME_THONGTIN);
						
						PropertyInfo maSoinfoThongTin = new PropertyInfo();
						maSoinfoThongTin.setName("MaSo");
						maSoinfoThongTin.setValue(maSo);
						requestThongTin.addProperty(maSoinfoThongTin);
						
						SoapSerializationEnvelope envelopeThongTin = new SoapSerializationEnvelope(
								SoapEnvelope.VER11);
						envelopeThongTin.setOutputSoapObject(requestThongTin);
						
						HttpTransportSE httpTransportSEThongTin = new HttpTransportSE(
								DangNhapVariable.URL_DANGNHAP);
						httpTransportSEThongTin.call(DangNhapVariable.SOAP_ACTION_THONGTIN, envelopeThongTin);
						
						SoapPrimitive responeThongTin = (SoapPrimitive) envelopeThongTin.getResponse();
						//Lưu thông tin vào các biến static
						ThongTinTaiKhoan.hoTen = responeThongTin.toString();
						ThongTinTaiKhoan.maTaiKhoan = maSo;
						return true;
					} catch (Exception e) {
						Log.i("hehe", e.toString());
						return false;
					}
					
				}else{
					return false;
				}
			} catch (Exception e) {
				Log.i("hehe", e.toString());
				return false;
			}
		}else{
			return false;
		}
	}

}
